package com.gitee.zycra.union.api.manager;

import com.gitee.zycra.union.api.common.model.ApiProject;
import com.gitee.zycra.union.api.common.param.AddProjectParam;
import com.gitee.zycra.union.api.common.param.UpdateProjectParam;
import com.gitee.zycra.union.api.common.util.IdGenerator;
import com.gitee.zycra.union.api.common.vo.ProjectDetailVO;
import com.gitee.zycra.union.api.common.vo.ProjectVO;
import com.gitee.zycra.union.api.dao.ApiProjectDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author zycra
 */
@Component
public class ApiProjectManager {

    @Autowired
    private ApiProjectDao apiProjectDao;

    @Autowired
    private IdGenerator idGenerator;

    /**
     * 添加项目
     *
     * @param addProjectParam 项目
     * @return 添加条数
     */
    public int addProject(AddProjectParam addProjectParam) {
        ApiProject apiProject = AddProjectParam.convertToApiProject(addProjectParam);
        apiProject.setId(idGenerator.nextId());
        return apiProjectDao.insert(apiProject);
    }

    /**
     * 根据ID更新项目
     *
     * @param updateProjectParam 项目
     * @return 更新条数
     */
    public int updateProject(UpdateProjectParam updateProjectParam) {
        return apiProjectDao.updateByIdAndGroupId(UpdateProjectParam.convertToApiProject(updateProjectParam));
    }

    /**
     * 根据ID删除项目
     *
     * @param groupId 项目组ID
     * @param id      项目ID
     * @return 更新结果
     */
    public int deleteProject(Long groupId, Long id) {
        return apiProjectDao.updateToDelete(groupId, id);
    }

    /**
     * 根据项目组ID查询项目
     *
     * @param groupId 项目组ID
     * @return 项目集合
     */
    public List<ProjectVO> projectList(Long groupId) {
        return apiProjectDao.selectByGroupId(groupId);
    }

    /**
     * 根据项目ID查询项目
     *
     * @param groupId   项目组ID
     * @param projectId 项目ID
     * @return 项目
     */
    public ProjectDetailVO projectDetail(Long groupId, Long projectId) {
        return apiProjectDao.selectProjectDetailById(groupId, projectId);
    }
}
